#include <QDebug>
#include <QMessageBox>
#include <QDir>
#include "configcapture.h"
#include "ui_configcapture.h"
#include "ewha.h"
#include "SettingHelper.h"

ConfigCapture::ConfigCapture(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ConfigCapture)
{
	ui->setupUi(this);

	connect(ui->controlPanel, SIGNAL(SignalJustCaptured(QString)), SIGNAL(SignalJustCaptured(QString)));
	connect(ui->fullTeethPanel, SIGNAL(SignalSelectedTeeth(int)), SIGNAL(SignalSelectedTeeth(int)));
	connect(ui->fullTeethPanel, SIGNAL(SignalSelectedTeeth(int)), SLOT(SlotTeethSelected(int)));
	connect(ui->controlPanel, SIGNAL(SignalExitCapture()), SIGNAL(SignalExitCapture()));
	connect(ui->controlPanel, SIGNAL(SignalStandby()), SLOT(SlotStandby()));
	connect(ui->controlPanel, SIGNAL(SignalReady()), SLOT(SlotReady()));
}

ConfigCapture::~ConfigCapture()
{
	delete ui;
}

void ConfigCapture::SlotCaptureStart()
{
	QMessageBox::information(this, "EWHA",
							 "치아를 선택하세요");
}

void ConfigCapture::SlotTeethSelected(int iTeeth)
{
	QString strMsg("%1 번 치아를 촬영합니다");
	emit SignalMessage(strMsg.arg(iTeeth));
	QMessageBox::information(this, "EWHA",
							 "수진자를 정렬하세요");
	QMessageBox::information(this, "EWHA",
							 "촬영을 시작합니다\n관전류, 조사시간 선택하세요");
	emit SignalStartConsole();
	QMessageBox::information(this, "EWHA",
							 "스텐바이를 누르세요");
}

void ConfigCapture::SlotStandby()
{
	QString strMsg("잠시 기다리세요. 촬영 준비 중입니다");
	emit SignalMessage(strMsg);
	emit SignalProgressStart();
	//send standby
	QTimer::singleShot(5000, this, SLOT(SlotTimerStandby())); //더미 타이머
}

void ConfigCapture::SlotReady()
{
	emit SignalProgressStart();
	//send ready
	QTimer::singleShot(5000, this, SLOT(SlotTimerReady())); //더미 타이머
	QTimer::singleShot(5000, this, SLOT(SlotTimerExpoOn())); //더미 타이머
	QTimer::singleShot(6000, this, SLOT(SlotTimerExpoOff())); //더미 타이머
	QTimer::singleShot(7000, this, SLOT(SlotTimerIMGDone())); //더미 타이머
}

void ConfigCapture::SlotError()
{

}

void ConfigCapture::SlotReceiveMessage(int i)
{
	switch (i)
	{
	case 1: //standbydone
		ui->controlPanel->setLamp(0, eStGreen);
		emit SignalMessage(QString());
		emit SignalProgressEnd();
		break;
	case 2: //readydone
		ui->controlPanel->setLamp(0, eStGray);
		ui->controlPanel->setLamp(1, eStGreen);
		emit SignalMessage(QString());
		emit SignalProgressEnd();
		break;
	case 3: //expoon
		ui->controlPanel->setLamp(1, eStGray);
		ui->controlPanel->setLamp(2, eStOrange);
		break;
	case 4: //expooff
		ui->controlPanel->setLamp(2, eStOrange);
		ui->controlPanel->setLamp(3, eStBlue);
		emit SignalProgressStart();
		break;
	case 5: //IMGDone
		emit SignalProgressEnd();
	{
		QString exePath;
		SettingHelper::loadConsole(&exePath);
		qDebug() << "[load exepath]" << exePath;
		QFileInfoList list = QFileInfo(exePath).dir().
				entryInfoList(QStringList() << "*.raw", QDir::Files, QDir::Time | QDir::Reversed);
		qDebug() << list.count();

		if(list.count() > 0)
		{
			emit SignalJustCaptured(list[0].absoluteFilePath());
		}
		else
		{
			//todo 오류, 파일이 생성되지 않음
		}
	}
	default:
		break;
	}
}

void ConfigCapture::SlotTimerStandby()
{
	this->SlotReceiveMessage(1);
}

void ConfigCapture::SlotTimerReady()
{
	this->SlotReceiveMessage(2);
}

void ConfigCapture::SlotTimerExpoOn()
{
	this->SlotReceiveMessage(3);
}

void ConfigCapture::SlotTimerExpoOff()
{
	this->SlotReceiveMessage(4);
}

void ConfigCapture::SlotTimerIMGDone()
{
	this->SlotReceiveMessage(5);
}
